package com.whf.android.jar.net.utils.no;


import android.graphics.Bitmap;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.whf.android.jar.base.delegate.IPageLoadListener;
import com.whf.android.jar.net.RestClient;
import com.whf.android.jar.net.callback.IError;
import com.whf.android.jar.net.callback.ISuccess;
import com.whf.android.jar.util.log.LatteLogger;

import java.util.WeakHashMap;

/**
 * 请求接口工具
 * <p>不使用拦截器，直接传
 *
 * @author qf
 * @version 2022/4/2
 */
public final class RestClientNoUtils implements IListenerNo {

    private final IPageLoadListener mPageLoadListener;
    private final IError mError;

    @NonNull
    public static RestClientNoUtils create() {
        return new RestClientNoUtils(null, null);
    }

    @NonNull
    public static RestClientNoUtils create(IError error) {
        return new RestClientNoUtils(null, error);
    }

    @NonNull
    public static RestClientNoUtils create(IPageLoadListener listener, IError error) {
        return new RestClientNoUtils(listener, error);
    }

    public RestClientNoUtils(IPageLoadListener listener, IError error) {
        this.mPageLoadListener = listener;
        this.mError = error;
    }

    @Override
    public final void onFailure() {
        if (mPageLoadListener != null) {
            mPageLoadListener.hideLoading();
        } else {
            ToastUtils.showShort("连接服务器失败，请稍后再试。");
        }
    }

    @Override
    public final void onError(int code, String msg) {
        if (mError != null) {
            mError.onError(code, msg);
        } else {
            if (mPageLoadListener != null) {
                mPageLoadListener.hideLoading();
            }
        }
    }

    @Override
    public void inGet(String url, ISuccess success) {
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        RestClient.builder()
                .url(url)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .getNo();
    }

    @Override
    public void inGet(String url, WeakHashMap<String, Object> headers, WeakHashMap<String, Object> params, ISuccess success) {
        LatteLogger.json(GsonUtils.toJson(params));
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        if (headers == null) {
            headers = new WeakHashMap<>();
        }
        RestClient.builder()
                .url(url)
                .headers(headers)
                .params(params)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .getNo();
    }

    @Override
    public void inPart(String url, WeakHashMap<String, Object> headers, WeakHashMap<String, Object> params, ISuccess success) {
        LatteLogger.json(GsonUtils.toJson(params));
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        if (headers == null) {
            headers = new WeakHashMap<>();
        }
        RestClient.builder()
                .url(url)
                .headers(headers)
                .params(params)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .partNo();
    }

    @Override
    public void inPost(String url, WeakHashMap<String, Object> headers, Object params, ISuccess success) {
        String data = "";
        if (ObjectUtils.isNotEmpty(params)) {
            if (params instanceof String) {
                data = (String) params;
            } else {
                data = GsonUtils.toJson(params);
            }
            LatteLogger.json(data);
        }
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        if (headers == null) {
            headers = new WeakHashMap<>();
        }
        RestClient.builder()
                .url(url)
                .headers(headers)
                .raw(data)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .postNo();
    }

    @Override
    public void inPost(String url, WeakHashMap<String, Object> headers, WeakHashMap<String, Object> params,
                       WeakHashMap<String, String> files, ISuccess success) {

        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        if (headers == null) {
            headers = new WeakHashMap<>();
        }
        RestClient.builder()
                .url(url)
                .headers(headers)
                .params(params)
                .fileSign(files)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .uploadMapFile();
    }

    @Override
    public void inQuery(String url, WeakHashMap<String, Object> headers,
                        WeakHashMap<String, Object> params, ISuccess success) {
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        if (headers == null) {
            headers = new WeakHashMap<>();
        }
        if (params == null) {
            params = new WeakHashMap<>();
        }
        RestClient.builder()
                .url(url)
                .headers(headers)
                .params(params)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .queryNo();
    }

    @Override
    public void inPut(String url, WeakHashMap<String, Object> headers, Object params, ISuccess success) {
        String data = "";
        if (ObjectUtils.isNotEmpty(params)) {
            if (params instanceof String) {
                data = (String) params;
            } else {
                data = GsonUtils.toJson(params);
            }
            LatteLogger.json(data);
        }
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        if (headers == null) {
            headers = new WeakHashMap<>();
        }
        RestClient.builder()
                .url(url)
                .headers(headers)
                .raw(data)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .putNo();
    }

    @Override
    public void inBitmap(String url, WeakHashMap<String, Object> headers, Bitmap params, ISuccess success) {
        if (mPageLoadListener != null) {
            mPageLoadListener.showLoading();
        }
        if (headers == null) {
            headers = new WeakHashMap<>();
        }
        RestClient.builder()
                .url(url)
                .headers(headers)
                .bitmap(params)
                .failure(this)
                .analysis(true)
                .success(success)
                .error(this)
                .build()
                .bitmapNo();
    }
}
